package api.result;

import java.io.PrintStream;

public class ResultKV<T> implements Result{
    private final String key;
    private final T value;

    public ResultKV(String key, T value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public Result union(Result another) {
        if (another == null) {
            return this;
        } else if (another instanceof ResultList) {
            return ((ResultList) another).add(this);
        } else if (another instanceof ResultKV) {
            ResultList list = new ResultList();
            list.add(this);
            list.add((ResultKV) another);
            return list;
        } else {
            throw new RuntimeException("Unknown result of " + another);
        }
    }

    @Override
    public void show() {
        System.out.println(this.toString());
    }

    @Override
    public void show(PrintStream printStream) {
        printStream.println(this.toString());
    }

    public String toString() {
        String ret =  key + "\t" + value.toString();
        return ret;
    }
}
